package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author terminal
 * @since 2023-06-07 22:58
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SpuManageServiceImpl implements SpuManageService {
    @Resource
    private SpuImageService spuImageService;
    @Resource
    private SpuInfoService spuInfoService;
    @Resource
    private SpuPosterService spuPosterService;
    @Resource
    private SpuSaleAttrService spuSaleAttrService;
    @Resource
    private SpuSaleAttrValueService spuSaleAttrValueService;
    @Resource
    private BaseSaleAttrService baseSaleAttrService;

    /**
     * 商品SPU分页
     *
     * @param spuInfoPage MP分页对象
     * @param category3Id 分类ID
     * @return
     */
    @Override
    public Page<SpuInfo> getSpuByPage(Page<SpuInfo> spuInfoPage, Long category3Id) {
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (category3Id != null) {
            queryWrapper.eq(SpuInfo::getCategory3Id, category3Id);
        }

        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        return spuInfoService.page(spuInfoPage, queryWrapper);
    }

    /**
     * 获取销售属性数据
     *
     * @return
     */
    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        return baseSaleAttrService.list();
    }

    /**
     * 保存商品SPU信息
     * 1.将提交商品SPU基本信息封装为SpuInfo对象,保存到spu_info表
     * 2.将提交多张商品图片封装图片集合对象List<SpuImage> 将图片批量保存
     * 3.将提交多张海报图片封装图片集合对象List<SpuPoster> 将海报图片批量保存
     * 4.将提交多个销售属性封装集合对象List<SpuSaleAttr> 将销售属性批量保存
     * 5.将提交每个销售属性包含属性值 封装集合对象List<SpuSaleAttrValue> 将销售属性值批量保存
     *
     * @param spuInfo
     * @return
     */
    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        if (spuInfo.getId() == null) {
            savaAndUpdateSpu(spuInfo);
        } else {
            spuInfoService.removeById(spuInfo.getId());
            savaAndUpdateSpu(spuInfo);
        }
    }

    /**
     * 保存和修改SPU
     * @param spuInfo
     */
    private void savaAndUpdateSpu(SpuInfo spuInfo) {
        // 1.将提交商品SPU基本信息封装为SpuInfo对象,保存到spu_info表
        spuInfoService.save(spuInfo);
        // 提取出ID  因为关联条件
        Long spuId = spuInfo.getId();
        // 2.将提交多张商品图片封装图片集合对象List<SpuImage> 将图片批量保存
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.stream().forEach(spuImage -> spuImage.setSpuId(spuId));
            spuImageService.saveBatch(spuImageList);
        }
        // 3.将提交多张海报图片封装图片集合对象List<SpuPoster> 将海报图片批量保存
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            spuPosterList.stream().forEach(spuPoster -> spuPoster.setSpuId(spuId));
            spuPosterService.saveBatch(spuPosterList);
        }
        // 4.将提交多个销售属性封装集合对象List<SpuSaleAttr> 将销售属性批量保存
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                spuSaleAttr.setSpuId(spuId);
                // 5.将提交每个销售属性包含属性值 封装集合对象List<SpuSaleAttrValue> 将销售属性值批量保存
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        spuSaleAttrValue.setSpuId(spuId);
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    });
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            });
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }

    /**
     * 根据SPUID查询商品销售属性列表(包含属性值)
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        //1.获取销售属性Mapper对象 调用自定义SQL
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        //2.返回结果
        return spuSaleAttrMapper.getSpuSaleAttrList(spuId);
    }

    /**
     * 用于修改回显
     *
     * @param spuId
     * @return
     */
    @Override
    public SpuInfo getSpuInfo(Long spuId) {
        SpuInfo spuInfo = spuInfoService.getById(spuId);

        LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<>();

        LambdaQueryWrapper<SpuSaleAttr> queryWrapper2 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SpuPoster> queryWrapper3 = new LambdaQueryWrapper<>();

        queryWrapper.eq(SpuImage::getSpuId, spuId);

        queryWrapper2.eq(SpuSaleAttr::getSpuId, spuId);
        queryWrapper3.eq(SpuPoster::getSpuId, spuId);
        spuInfo.setSpuImageList(spuImageService.list(queryWrapper));

        spuInfo.setSpuSaleAttrList(spuSaleAttrService.list(queryWrapper2));
        spuInfo.setSpuPosterList(spuPosterService.list(queryWrapper3));

        return spuInfo;
    }
}
